package com.sy.sevice.impl;

import com.sy.constant.RegexConstant;
import com.sy.dto.JsonResult;
import com.sy.entity.Teacher;
import com.sy.mapper.TeacherMapper;
import com.sy.sevice.TeacherService;
import com.sy.utils.CommonUtils;
import com.sy.vo.InputFormVo;
import com.sy.vo.ModifyTeacherVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Yasuda-T
 * @version 1.0
 * @date 2020/9/14 13:43
 * @usage 老师类的相关的Service层
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public JsonResult addTeacher(InputFormVo addUserVo) {
        Teacher teacher = new Teacher();
        teacher.setUsername(addUserVo.getUsername());
        if (null == addUserVo.getPassword()) {
            teacher.setPassword("admin");
        } else {
            teacher.setPassword(addUserVo.getPassword());
        }
        teacher.setGender(addUserVo.getGender());
        Integer rows = teacherMapper.addTeacher(teacher);
        System.out.println("受影响的行数：" + rows);
        return JsonResult.buildSuccess(200, teacher);
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public JsonResult modifyTeacher(ModifyTeacherVo modifyTeacherVo) {
        String username = modifyTeacherVo.getUsername();
        String oldPwd = modifyTeacherVo.getOldPwd();
        String password = modifyTeacherVo.getPassword();
        String newPwdConfirm = modifyTeacherVo.getNewPwdConfirm();
        String phone = modifyTeacherVo.getPhone();
        String email = modifyTeacherVo.getEmail();


        //TODO:获得当前登入学生的id
        modifyTeacherVo.setId(1);

        //用户名
        //判断
        if (StringUtils.isBlank(username)) {
            return JsonResult.buildFailure(400, "用户名不能为空");
        }
        if (!CommonUtils.validateRegex(username, RegexConstant.USER_NAME_REGEX)) {
            return JsonResult.buildFailure(400, "用户名不合法");
        }
        //手机号码
        if (StringUtils.isBlank(phone)) {
            return JsonResult.buildFailure(400, "手机号不能为空");
        }
        if (!CommonUtils.validateRegex(phone, RegexConstant.PHONE_NUMBER_REGEX)) {
            return JsonResult.buildFailure(400, "手机号不合法");
        }
        //当前密码
        //获得当前老师的密码
        if (StringUtils.isBlank(oldPwd)) {
            return JsonResult.buildFailure(400, "密码不能为空");
        }
        //TODO:根据老师id查询密码
        String pwdByTeacherId = teacherMapper.getPwdByTeacherId(1);

        if (!oldPwd.equals(pwdByTeacherId)) {
            return JsonResult.buildFailure(400, "输入的密码不对");
        }
        //新密码
        if (StringUtils.isBlank(password)) {
            return JsonResult.buildFailure(400, "密码不能为空");
        }
        if (!password.equals(newPwdConfirm)) {
            return JsonResult.buildFailure(400, "两次输入的密码不一致");
        }
        if (!CommonUtils.validateRegex(password, RegexConstant.STRONG_PWD_REGEX)) {
            return JsonResult.buildFailure(400, "密码不是强密码");
        }
        //邮箱
        if (StringUtils.isBlank(email)) {
            return JsonResult.buildFailure(400, "邮箱不能为空");
        }
        if (!CommonUtils.validateRegex(email, RegexConstant.EMAIL_NUMBER_REGEX)){
            return JsonResult.buildFailure(400, "邮箱不合法");
        }
        //将修改后的信息更新到数据库
        boolean isSuccess = teacherMapper.updateTeacher(modifyTeacherVo);
        if (isSuccess) {
            return JsonResult.buildSuccess(200, modifyTeacherVo);
        }
        return JsonResult.buildFailure(400, "修改个人信息失败");

    }
}

